package com.zretc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zretc.dao.OrderAndCollectionDao;
import com.zretc.entity.CollectionInfo;
import com.zretc.entity.HomeInfo;
import com.zretc.entity.OrderInfo;
import com.zretc.entity.RoomInfo;
import com.zretc.entity.UserInfo;
import com.zretc.util.DBUtil;
import com.zretc.util.PageInfo;

/**
 * @author 赵懒洋
 *
 */
public class OrderAndCollectionDaoImpl implements OrderAndCollectionDao{
	
	@Override
	public int deleteOrderByOrderNo(String orderNo) {
		int n = DBUtil.doUpdate("delete from orderinfo where order_id = ?", orderNo);
		return n;
	}

	/**
	 * 删除个人中心的收藏信息
	 * @author 赵懒洋
	 * @param collection
	 * @return
	 */
	@Override
	public int deleteCollectionByCollectionNo(String collectionNo) {
		int n = DBUtil.doUpdate("delete from collectioninfo where collection_id = ?", collectionNo);
		return n;
	}
	
	/**
	 * 根据不同条件,分页查询订单信息
	 * @author 赵懒洋
	 * @param homeName + roomId  订单名称（民宿名+房号）
	 * @param pageNum   当前页
	 * @param pageSize  获取的条数
	 * @param num       num = 1(前台的订单信息)  num = 2(后台的订单信息)
	 * @return PageInfo 分页结果集  
	 */
	@Override
	public PageInfo<OrderInfo> findOrderByPage(Map<String, String> params) {
		List<OrderInfo> list = new ArrayList<>();
		OrderInfo orderInfo = null;
		PageInfo<OrderInfo> pageInfo = null;
		StringBuilder sb = new StringBuilder();
		
		String homeNameKey = (String) params.get("homeName");
		Integer num = Integer.valueOf(params.get("num"));
		Integer pageNum = Integer.valueOf(params.get("pageNum"));
		Integer pageSize =Integer.valueOf( params.get("pageSize"));
		
		ResultSet rs = null;
		
		// 判断传入的参数是 1 那就是前台
		// 如果参数是 2 那就是后台
		if(1 == num) {
			Integer userNo = Integer.valueOf(params.get("userNo"));
			sb.append("select ")
			.append(" a.order_id,c.home_name,b.room_id,b.room_price,a.order_state ")
			.append(" from orderinfo a ")
			.append(" inner join roominfo b ")
			.append(" on a.room_id = b.room_id ")
			.append(" inner join homeinfo c ")
			.append(" on b.home_id = c.home_id ")
			.append(" where c.home_name like ? and a.user_id = ?")
			.append(" limit ?,? ");
			rs = DBUtil.doQuery(sb.toString(),homeNameKey,userNo,(pageNum-1)*pageSize,pageSize);
			try {
				while (rs.next()) {
							
					String orderNo = rs.getString("order_id");
					String homeName = rs.getString("home_name");
					String roomNo = rs.getString("room_id");
					Integer roomPrice = rs.getInt("room_price");
					int orderState = rs.getInt("order_state");
					// 获取前台的订单信息：订单编号（民宿编号+房间号+日期）订单名称（民宿名+房号） 
					// 总价格 支付状态（未支付 去支付） 添加时间
					orderInfo = new OrderInfo(orderNo, orderState, null, null, new UserInfo(userNo, null, null, null, null, null), new RoomInfo(roomNo, null, roomPrice, new HomeInfo(null, homeName, null, null, null), null));
					list.add(orderInfo);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Integer total = 0;
			
			// 查询分页前的总记录数
			sb = new StringBuilder();		
			sb.append("select count(*)")
			.append(" from orderinfo a")
			.append(" inner join roominfo b")
			.append(" on a.room_id = b.room_id")
			.append(" inner join homeinfo c")
			.append(" on b.home_id = c.home_id ")
			.append(" where c.home_name like ? and a.user_id = ?");
					
			rs = DBUtil.doQuery(sb.toString(),homeNameKey,userNo);
			try {
				while (rs.next()) {
					total  = rs.getInt(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
			
		}else if (2 == num) {
			sb.append("select ")
			.append(" a.order_id,c.home_name,b.room_id,b.room_price,d.user_name,a.order_state ")
			.append(" from userinfo d ")
			.append(" inner join orderinfo a ")
			.append(" on a.user_id = d.user_id ")
			.append(" inner join roominfo b ")
			.append(" on a.room_id = b.room_id ")
			.append(" inner join homeinfo c ")
			.append(" on b.home_id = c.home_id ")
			.append(" where c.home_name like ?")
			.append(" limit ?,? ");
			rs = DBUtil.doQuery(sb.toString(),homeNameKey,(pageNum-1)*pageSize,pageSize); 
			try {
				while (rs.next()) {
							
					String orderNo = rs.getString("order_id");
					String homeName = rs.getString("home_name");
					String roomNo = rs.getString("room_id");
					Integer roomPrice = rs.getInt("room_price");
					String userName = rs.getString("user_name");
					int orderState = rs.getInt("order_state");
					// 获取后台的订单信息：订单编号 订单名称 总价格 购买人 支付状态 添加时间
					orderInfo = new OrderInfo(orderNo, orderState, null, null, new UserInfo(null, userName, null, null, null, null), new RoomInfo(roomNo, null, roomPrice, new HomeInfo(null, homeName, null, null, null), null));
					list.add(orderInfo);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Integer total = 0;
			
			// 查询分页前的总记录数
			sb = new StringBuilder();		
			sb.append("select count(*)")
			.append(" from orderinfo a")
			.append(" inner join roominfo b")
			.append(" on a.room_id = b.room_id")
			.append(" inner join homeinfo c")
			.append(" on b.home_id = c.home_id ")
			.append(" where c.home_name like ?");
					
			rs = DBUtil.doQuery(sb.toString(),homeNameKey);
			try {
				while (rs.next()) {
					total  = rs.getInt(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
		}
				
		
		return pageInfo;
	}
	
	/**
	 * 根据不同条件,分页查询收藏信息
	 * @author 赵懒洋
	 * @param homeName  民宿的名字
	 * @param pageNum   当前页
	 * @param pageSize  获取的条数
	 * @return PageInfo 分页结果集
	 */
	@Override
	public PageInfo<CollectionInfo> findCollectionByPage(Map<String, String> params) {
		List<CollectionInfo> list = new ArrayList<>();
		
		StringBuilder sb = new StringBuilder();
		
		//（收藏编号(homeid+userid) + 收藏的民宿 
		sb.append(" select ")
		  .append(" b.collection_id,d.home_id,a.user_id,d.home_name ")
		  .append(" from userinfo a ")
		  .append(" inner join collectioninfo b ")
		  .append(" on a.user_id = b.user_id ")
		  .append(" inner join homeinfo d ")
		  .append(" on b.home_id = d.home_id ")
		  .append(" where d.home_name like ? ")
		  .append(" limit ?,?");
		
		String homeNameKey = params.get("homeName");
		Integer pageNum = Integer.valueOf(params.get("pageNum"));
		Integer pageSize =Integer.valueOf( params.get("pageSize"));
		
		ResultSet rs = DBUtil.doQuery(sb.toString(),homeNameKey,(pageNum-1)*pageSize,pageSize);
		try {
			while (rs.next()) {
				String collectionNo = rs.getString("collection_id");
				String homeNo = rs.getString("home_id");
				Integer userNo = rs.getInt("user_id");
				String homeName = rs.getString("home_name");
				
				CollectionInfo collectionInfo = new CollectionInfo(collectionNo, new UserInfo(userNo, null, null, null, null, null), null, new HomeInfo(homeNo, homeName, null, null, null));
				list.add(collectionInfo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Integer total = 0;
		
		// 查询分页前的总记录数
		sb = new StringBuilder();		
		sb.append("select count(*)")
		  .append(" from collectioninfo b ")
		  .append(" inner join homeinfo d")
		  .append(" on b.home_id = d.home_id")
		  .append(" where d.home_name like ?");
				
		rs = DBUtil.doQuery(sb.toString(),homeNameKey);
		try {
			while (rs.next()) {
				total  = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		PageInfo<CollectionInfo> pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
		return pageInfo;
	}
}
